`uri_escape`: Do not fail when nil is given.

Do just the same as the standard filter `url_encode`.

Akinori MUSHA 10 years ago
parent
commit
64802bd457
2 changed files with 5 additions and 1 deletions
  1. 1 1
      app/concerns/liquid_interpolatable.rb
  2. 4 0
      spec/concerns/liquid_interpolatable_spec.rb

+ 1 - 1
app/concerns/liquid_interpolatable.rb

@@ -47,7 +47,7 @@ module LiquidInterpolatable
47 47
   # Ref: http://tools.ietf.org/html/rfc3986#page-12
48 48
   module Filters
49 49
     def uri_escape(string)
50
-      CGI::escape string
50
+      CGI.escape(string) rescue string
51 51
     end
52 52
   end
53 53
   Liquid::Template.register_filter(LiquidInterpolatable::Filters)

+ 4 - 0
spec/concerns/liquid_interpolatable_spec.rb

@@ -11,6 +11,10 @@ describe LiquidInterpolatable::Filters do
11 11
     it 'should escape a string for use in URI' do
12 12
       @filter.uri_escape('abc:/?=').should == 'abc%3A%2F%3F%3D'
13 13
     end
14
+
15
+    it 'should not raise an error when an operand is nil' do
16
+      @filter.uri_escape(nil).should be_nil
17
+    end
14 18
   end
15 19
 
16 20
   describe 'validations' do